package com.qidian.buy.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qidian.buy.sys.entity.Menu;
import com.qidian.buy.sys.mapper.MenuMapper;
import com.qidian.buy.sys.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidian.buy.util.pojo.QdResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2024-05-07
 */
@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public QdResult<List<Menu>> queryAll() {
        IMenuService menuService = (IMenuService) AopContext.currentProxy();
        return QdResult.success(menuService.queryByParentId(0L));
    }

    public List<Menu> queryByParentId(Long parentId){
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_parent_id",parentId);
        List<Menu> menus = menuMapper.selectList(wrapper);
        if(menus == null || menus.size() == 0){
            return null;
        }
        IMenuService menuService = (IMenuService) AopContext.currentProxy();
        for (Menu menu : menus){
            menu.setChildren(menuService.queryByParentId(menu.getMenuId()));
        }
        return menus;
    }
}
